package com.atguigu.scala.test

import java.text.SimpleDateFormat
import java.time.format.DateTimeFormatter
import java.util.Date
import scala.io.Source

/**
 * @author lizikuan
 * @date 2021/5/25 15:55
 */
object Test3 {
  def main(args: Array[String]): Unit = {
    val datas = Source.fromFile("./datas/taxi.txt").getLines().toList
     datas.map(_.split("\t").toList)
      .groupBy(line => line.head)
      .map(x => {
        x._1 ->
          x._2.sortBy(_.apply(6))
      }).map(x => {
      x._1 ->
        x._2.map(y => {
          val region = y.apply(1)
          val time1 = y.apply(4)
          val time2 = y.apply(6)
          (region, time1, time2)
        })
    })
      .map(x => {
        val list = x._2
        val range = x._2.size

        for (i <- 1 until range) yield {
          val begin = new SimpleDateFormat("HH:mm:ss").parse(list(i)._2).getTime
          val end = new SimpleDateFormat("HH:mm:ss").parse(list(i - 1)._3).getTime
          (list(i)._1, begin - end)
        }
      })
      .flatMap(x => x.toList).groupBy(_._1).map(x => {

      val sum = x._2.map(x => {
        x._2
      }).toList.sum
      val count = x._2.map(x => {
        x._2
      }).toList.size
      x._1 -> (sum, count)
    })
      .map(x => {
        val waitTime: Long = x._2._1 / x._2._2

        x._1 -> tranTimeToString(waitTime.toString)
      }).foreach(println(_))
  }

  def tranTimeToString(tm: String): String = {
    val fm = new SimpleDateFormat("mm:ss")
    val tim = fm.format(new Date(tm.toLong))
    tim
  }
}
